package com.krlin.soccer.soccer.controller;

import com.krlin.soccer.soccer.entity.Queue;
import com.krlin.soccer.soccer.service.QueueService;
import com.krlin.soccer.soccer.tools.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class QueueController {

    @Autowired
    QueueService queueService;

    /**
     * 查看所有队伍信息
     * @return
     */
    @GetMapping("/queues")
    public Result getQueue() {
        return Result.succ(queueService.getQueues());
    }

    /**
     * 根据队伍ID查询队伍
     * @param id
     * @return
     */
    @GetMapping("/queue/{id}")
    public Result getQueueById(@PathVariable(name = "id") Integer id) {
        return Result.succ(queueService.getQueueById(id));
    }

    /**
     * 插入一条队伍数据
     * @param queue
     * @return
     */
    @PostMapping("/queue/insert")
    public Result insertQueue(@RequestBody Queue queue) {
        int res = queueService.insertQueue(queue);
        if (res == 1) {
            return Result.succ(200, "添加成功", null);
        } else {
            return Result.fail("添加队伍失败");
        }
    }

    /**
     * 修改队伍信息
     * @param queue
     * @return
     */
    @PutMapping("/queue/edit/{id}")
    public Result updateQueueById(@PathVariable(name = "id") Integer id, @RequestBody Queue queue) {
        log.info("修改队伍信息"+queue.toString());
        queue.setQueueId(id);
        int res = queueService.updateQueue(queue);
        if (res == 1) {
            return Result.succ(queue);
        } else {
            return Result.fail("修改队伍信息失败");
        }
    }

    /**
     * 删除队员信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/queue/{id}", method = RequestMethod.DELETE)
    public Result deleteQueue(@PathVariable(name = "id") Integer id) {
        int res = queueService.deleteQueue(id);
        if (res == 1) {
            return Result.succ(200, "删除成功", null);
        } else {
            return Result.fail("删除球队信息失败");
        }
    }
}
